package example.com.widgetdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qiaoshouqing on 2017/8/30.
 */

public class NormalRecyclerViewAdapter extends RecyclerView.Adapter<NormalRecyclerViewAdapter.NormalTextViewHolder> {
    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private List<BGEntity> bgEntities = new ArrayList<>();

    public NormalRecyclerViewAdapter(Context context, List<BGEntity> bgEntities) {
        this.bgEntities = bgEntities;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NormalTextViewHolder(mLayoutInflater.inflate(R.layout.bg_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final NormalTextViewHolder holder, final int position) {
        holder.tv_name.setText(bgEntities.get(position).getDescribe());
        holder.iv_bg.setImageResource(bgEntities.get(position).getDrawableResource());

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    mOnItemClickLitener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return bgEntities == null ? 0 : bgEntities.size();
    }

    public static class NormalTextViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_name;
        public ImageView iv_bg;
        public NormalTextViewHolder(View view){
            super(view);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            iv_bg = (ImageView) view.findViewById(R.id.iv_bg);
        }
    }
}